# boston，降维4维
from sklearn.datasets import load_boston
from sklearn.decomposition import PCA
from sklearn.model_selection import train_test_split
from tensorflow.keras import layers, models, losses, optimizers

data = load_boston()
x = data.data
y = data.target

#降维到4
x = PCA(n_components=4).fit_transform(x)

# 训练集和测试集
x_train, x_test, y_train, y_test = train_test_split(x, y)

# 2个隐藏层   8， 4
model = models.Sequential([
    layers.Dense(8, input_dim=(4), activation='relu'),
    layers.Dense(4, activation='relu'),
    layers.Dense(1)
])

#模型配置
model.compile(optimizer=optimizers.Adadelta(lr=0.1), loss=losses.mean_squared_error)

model.fit(x_train, y_train, validation_data=(x_test, y_test), epochs=2000)  #验证集validation_data